package masterPackage;

public class Scanner {

	final String NO_ERROR = "NO_ERROR";
	final String ERROR = "ERROR";

	final String PLUS = "PLUS";
	final String MINUS = "MINUS";
	final String MAL = "MAL";
	final String DURCH = "DURCH";
	final String POTENZ = "POTENZ";
	final String KLAMMER_AUF = "KLAMMER_AUF";
	final String KLAMMER_ZU = "KLAMMER_ZU";
	final String ENDE = "ENDE";

	final String CONST = "KONSTANTE";
	private boolean isSysoutsAnScanner = false;

	public Scanner() {
	}

	private String token;
	int index = 0;

	public GelesenesElement scanString(String input) {
		String lexem = "";
		int errorCode = 0;
		while (input.charAt(index) != ';') {
			switch (input.charAt(index)) {
			case ' ':
				index++;
				break;
			case '+':
				lexem = String.valueOf(input.charAt(index));
				token = PLUS;
				if (isSysoutsAnScanner) {
					System.out.println("token: " + token + " lexem: " + lexem
							+ " Error-Code: " + errorCode);
				}
				index++;
				return new GelesenesElement(lexem, PLUS, errorCode);
			case '-':
				lexem = String.valueOf(input.charAt(index));
				token = MINUS;
				if (isSysoutsAnScanner) {
					System.out.println("token: " + token + " lexem: " + lexem
							+ " Error-Code: " + errorCode);
				}
				index++;
				return new GelesenesElement(lexem, MINUS, errorCode);
			case '/':
				lexem = String.valueOf(input.charAt(index));
				token = DURCH;
				if (isSysoutsAnScanner) {
					System.out.println("token: " + token + " lexem: " + lexem
							+ " Error-Code: " + errorCode);
				}
				index++;
				return new GelesenesElement(lexem, DURCH, errorCode);
			case '*':
				lexem = String.valueOf(input.charAt(index));
				token = MAL;
				if (isSysoutsAnScanner) {
					System.out.println("token: " + token + " lexem: " + lexem
							+ " Error-Code: " + errorCode);
				}
				index++;
				return new GelesenesElement(lexem, MAL, errorCode);
			case '^':
				lexem = String.valueOf(input.charAt(index));
				token = POTENZ;
				if (isSysoutsAnScanner) {
					System.out.println("token: " + token + " lexem: " + lexem
							+ " Error-Code: " + errorCode);
				}
				index++;
				return new GelesenesElement(lexem, POTENZ, errorCode);
			case '(':
				lexem = String.valueOf(input.charAt(index));
				token = KLAMMER_AUF;
				if (isSysoutsAnScanner) {
					System.out.println("token: " + token + " lexem: " + lexem
							+ " Error-Code: " + errorCode);
				}
				index++;
				return new GelesenesElement(lexem, KLAMMER_AUF, errorCode);
			case ')':
				lexem = String.valueOf(input.charAt(index));
				token = KLAMMER_ZU;
				if (isSysoutsAnScanner) {
					System.out.println("token: " + token + " lexem: " + lexem
							+ " Error-Code: " + errorCode);
				}
				index++;
				return new GelesenesElement(lexem, KLAMMER_ZU, errorCode);
			default:
				if (Character.isDigit(input.charAt(index))) {
					boolean valid = true;
					boolean isKommaGesetzt = false;
					token = CONST;
					lexem = String.valueOf(input.charAt(index));
					while (valid) {
						if (Character.isDigit(input.charAt(index + 1))) {
							lexem += String.valueOf(input.charAt(index + 1));
							valid = true;
						} else if (input.charAt(index + 1) == '.'
								&& !isKommaGesetzt) {
							lexem += String.valueOf(input.charAt(index + 1));
							valid = true;
							isKommaGesetzt = true;
						} else {
							valid = false;
						}
						index++;
					}
					if (isSysoutsAnScanner) {
						System.out.println("token: " + token + " lexem: "
								+ lexem + " Error-Code: " + errorCode);
					}
					return new GelesenesElement(lexem, CONST, errorCode);

				} else {
					errorCode = 1;
					lexem = String.valueOf(input.charAt(index));
					if (isSysoutsAnScanner) {
						System.out.println("          lexem: " + lexem
								+ "Fehlerhaftes Zeichen: "
								+ input.charAt(index) + " an Stelle " + index);
					}
					errorCode = 0;
					index++;
				}
				break;
			}
		}
		lexem = String.valueOf(input.charAt(index));

		if (isSysoutsAnScanner) {
			System.out.println("token: " + ENDE + " lexem: " + lexem
					+ "Fehlerhaftes Zeichen: " + input.charAt(index)
					+ " an Stelle " + index);
		}
		return new GelesenesElement(lexem, ENDE, errorCode);
	}
}